package com.nlelpct.security.handler;


import com.nlelpct.constant.ResponseCode;
import com.nlelpct.enums.ResponseEnum;
import com.nlelpct.model.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @author :小马
 * @date : 2023-07-27
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = NullPointerException.class)
    public ResponseResult<String> nullPointerExceptionHandler(NullPointerException e) {
        log.error("捕获到空指针异常: {}", e.getMessage());
        return ResponseResult.error(ResponseCode.ORDINARY_FAIL, "出现了空指针异常,请联系系统管理员");
    }

    @ExceptionHandler(value = AccessDeniedException.class)
    public ResponseResult<String> accessDeniedExceptionHandler(AccessDeniedException e) {
        log.error("捕获到权限不足异常: {}", e.getMessage());
        return ResponseResult.error(ResponseEnum.NO_PERMISSION_ENUM);
    }

    @ExceptionHandler(value = Exception.class)
    public ResponseResult<String> exceptionHandler(Exception e) {
        log.error("捕获到未定义异常: {}", e.getMessage());
        return ResponseResult.error(ResponseCode.ORDINARY_FAIL, "出现了未知异常,请联系系统管理员");
    }

}
